var nowPage = 0;
var planManager = {
	getData : function() {
		var name = $("#planName").val();
		var startTime = $("#startTime").val();
		var endTime = $("#endTime").val();
		checkTime(startTime, endTime);
		startTime = $("#startTime").val();
		endTime = $("#endTime").val();
		_planManagerGetData = $("#pageShow").myPagination({
			currPage : 1,
			pageCount : 10,
			pageNumber : 5,
			panel : {
				tipInfo_on : true,
				links : "javascript:void()"
			},
			ajax : {
				onClick : function(page) {
					ZENG.msgbox.show(" 正在加载" + page + "页，请稍后...", 6, 600);
					nowPage = page;
				},
				on : true,
				type : "POST",
				dataType : 'json',
				url : "/app/plan!getPlanListByNameAndPage",
				param : {
					"planName" : name,
					"startTime" : startTime,
					"endTime" : endTime
				},
				callback : function(data) {
					backUrl.addCookie(data.state);
					if ('success' == data.state) {
						pageCountId = data.pageCount;
						planManager.createContent(data.msg);
					} else if (data.state == "error") {
						errorAlert(data.msg);
					}
				}
			}
		});
	},
	createContent : function(data) {
		var ds = $("#dataShow");
		var content = "";
		ds.empty();
		if (data != null && data.length != 0) {
			$("#pageShow").show();
			content = "<tr><th width='51%' scope='col'>计划名称</th>" + "<th width='23%' scope='col'>录入时间</th>"
					+ "<th width='26%' scope='col'>操作</th></tr>";
			for ( var int = 0; int < data.length; int++) {
				var v = data[int];
				var id = v.id;
				var name = v.name;
				var time = v.inputTime;
				var count = v.countProject;
				
				var _name = name;
				if (name.length > 33) {
					name = name.substring(0, 32) + "...";
				}
				time = time.substring(0, 19);
				content += "<tr><td align='center'><a href='/app/plan!jumpPlanDetails?planId=" + id
						+ "' target='_blank' title='" + _name + "' class='tempClass'>" + name + "（" + count
						+ "项）</a></td><td align='center'>" + time
						+ "</td><td align='center'><a href='javascript:updatePlan.show(\"" + name + "\","
						+ id + ")'>修改</a><a href='javascript:deletePlan(" + id
						+ ")'>删除</a><a href='/app/plan!jumpPlanDetails?planId=" + id
						+ "' target='_blank'>详情</a><a href='/app/plan!planExcelByPlanId?planId=" + id
						+ "' target='_blank'>导出Excel</a></td></tr>";
			}
		} else {
			$("#pageShow").hide();
			content = "<tr id='noData'><td align='center'><h1>暂无数据</h1></td></tr>";
		}
		ds.append(content);
		planManagerPoshytip();
	},
	jumpPage : function() {// 分页跳转
		_planManagerGetData.jumpPage(nowPage);
	}
};
/**
 * 提示层
 */
function planManagerPoshytip() {
	$(".tempClass").poshytip({
		className : 'tip-darkgray',
		showOn : 'hover',
		alignTo : 'target',
		alignX : 'right',
		alignY : "center",
		offsetX : 10,
		allowTipHover : true,
		fade : true,
		slide : true
	});
}
var addPlan = {
	show : function() {
		art.dialog({
			id : "addPlan",
			lock : true,
			content : "计划名称：<input type='text' id='addPlanName'>",
			button : [ {
				name : '确定',
				callback : function() {
					var name = $("#addPlanName").val();
					if (null == name || 0 == name.length) {
						alert("请填写计划名称！");
						return false;
					} else if (checkSC(name)) {
						alert("计划名称不能含有特殊字符！");
						return false;
					} else {
						addPlan.add(name);
						art.dialog({
							id : "addPlan"
						}).close();
					}
				},
				focus : true
			} ],
			cancelVal : '取消',
			cancel : true
		});
	},
	add : function(name) {
		$.ajax({
			dataType : "json",
			type : "post",
			data : "planName=" + name,
			url : "/app/plan!addPlan",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state == "success") {
					tipsAlert(data.msg);
					nowPage = 1;
					planManager.jumpPage();
				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	}
};
/**
 * 修改计划
 */
var updatePlan = {
	show : function(name, id) {
		art.dialog({
			id : "updataPlan",
			lock : true,
			content : "计划名称：<input type='text' id='updatePlanName' value='" + name + "'>",
			button : [ {
				name : '确定',
				callback : function() {
					var name = $("#updatePlanName").val();
					if (null == name || 0 == name.length) {
						alert("请填写计划名称！");
						return false;
					} else if (checkSC(name)) {
						alert("计划名称不能含有特殊字符！");
						return false;
					} else {
						updatePlan.update(name, id);
						art.dialog({
							id : "updataPlan"
						}).close();
					}
				},
				focus : true
			} ],
			cancelVal : '取消',
			cancel : true
		});
	},
	update : function(name, id) {
		$.ajax({
			dataType : "json",
			type : "post",
			data : "planName=" + name + "&planId=" + id,
			url : "/app/plan!updatePlan",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state == "success") {
					tipsAlert(data.msg);
					planManager.jumpPage();
				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	}
};
/**
 * 删除计划
 * 
 * @param id
 */
function deletePlan(id) {
	art.dialog.confirm('确认删除？', function() {
		$.ajax({
			dataType : "json",
			type : "post",
			data : "planId=" + id,
			url : "/app/plan!deletePlan",
			success : function(data) {
				backUrl.addCookie(data.state);
				if (data.state == "success") {
					tipsAlert(data.msg);
					planManager.jumpPage();
				} else if (data.state == "error") {
					errorAlert(data.msg);
				}
			}
		});
	});
}
function planManagerOnload() {
	planManager.getData();
	$("#startTime").focus(function() {
		WdatePicker({
			dateFmt : 'yyyy-MM-dd'
		});
	});
	$("#endTime").focus(function() {
		WdatePicker({
			dateFmt : 'yyyy-MM-dd'
		});
	});
	$("#search").click(function() {
		planManager.getData();
	});
}